<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="app">
    </div>

    <!-- 父组件 -->
    <template id="one">

        <!-- ref 属性可以注册元素或子组件的引用，
            所有被注册的组件实例将保存在组件的 this.$refs 对象中 -->

        <div ref="parent_ref">{{message}}</div>
        <div>{{num}}</div>
        <button @click="add">父按钮</button>
        <hr>
        <two ref="son_ref"></two>
    </template>
    <!-- 子组件 -->
    <template id="two">
        <div>{{message}}</div>
        <div>{{num}}</div>
        <button @click="add">子按钮</button>
    </template>
</body>
<script>
    const twoConfig = {
        template: '#two',
        data() {
            return {
                message: '子组件信息',
                num: 0
            }
        },
        methods: {
            add() {
                this.num++;
            }
        },
        mounted() {
            //通过 $parent 获取父组件的实例对象
            console.log(this.$parent)
            // 通过 $root 获取根组件的实例对象
            console.log(this.$root)
            this.$root.add();
            console.log(this.$parent.num);
        }
    }

    const HelloVueApp = {
        template: '#one',
        data() {
            return {
                message: '父组件信息',
                num: 0
            }
        },
        methods: {
            add() {
                this.num++;
            }
        },
        components: {
            two: twoConfig
        }
    }
    Vue.createApp(HelloVueApp).mount('#app')
</script>

</html>